<?php
class ModelTotalGiftcard extends Model {
	public function getTotal(&$total_data, &$total, &$taxes, $status = 0) { 
		if ($this->config->get('giftcard_status') && $status) {  
			if (isset($this->session->data['giftcard'])) {
				$this->load->language('total/giftcard');			
				$this->load->model('checkout/giftcard'); 
								
				$giftcard_info = $this->model_checkout_giftcard->getGiftcard($this->session->data['giftcard']);				
				if ($giftcard_info) {
					if ($giftcard_info['amount'] > $total) {
						$amount = $total;	
					} else {
						$amount = $giftcard_info['amount'];	
					}				
					
					$total_data['giftcard'] = array(
						'code'       => 'giftcard',
						'title'      => ($this->config->get('giftcard_title') != '') ? $this->config->get('giftcard_title') : $this->language->get('text_giftcard'),
						'text'       => $this->currency->formatPrice(-$amount),
						'value'      => -$amount,
						'sort_order' => $this->config->get('giftcard_sort_order'),
						'total_code' => $giftcard_info['code'],
					);
	
					$total -= $amount;
				} 
			}
		}
	}
	
	public function confirm($order_id, $order_total) {
		$code = $order_total['total_code'];
		
		$this->load->model('checkout/giftcard');		
		$giftcard_info = $this->model_checkout_giftcard->getGiftcard($code);
		
		if ($giftcard_info) {
			$this->model_checkout_giftcard->redeem($giftcard_info['giftcard_id'], $order_id, $order_total['value']);	
		}						
	}	
}
?>